package org.xu.config;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.region.Region;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class CosClientManager implements DisposableBean {

    private final COSClient cosClient;

    public CosClientManager() {
        // 初始化 COSCredentials
        COSCredentials cred = new BasicCOSCredentials("AKIDh4a1fotCQflqOrDRl5FLORnOtBQ80y9O", "Rj3KWoGmZbp5LXgyBPUO720qXltAkTZn");
        // 设置 bucket 的区域
        ClientConfig clientConfig = new ClientConfig(new Region("ap-beijing"));
        // 生成 cos 客户端
        this.cosClient = new COSClient(cred, clientConfig);
    }

    public COSClient getCosClient(){
        return cosClient;
    }

    @Override
    public void destroy() throws Exception {
        log.info("销毁 COSClient");
        if (cosClient != null) {
            cosClient.shutdown();
        }
    }
}
